package com.newly.center.business.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import com.newly.api.business.feign.StatisticsClient;
import com.newly.center.business.service.StatisticsService;
import com.newly.common.base.constants.CodeConstant;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.auth.ICurrentTenant;
import com.newly.common.base.entity.business.Dto.StatisticsDto;
import com.newly.common.base.entity.business.Vo.ExcelAreaData;
import com.newly.common.base.entity.business.Vo.ExcelAxleData;
import com.newly.common.base.entity.business.Vo.ExcelSiteData;
import com.newly.common.base.entity.business.Vo.ExcelTimeData;
import com.newly.common.base.exception.NewlyException;
import com.newly.common.core.utils.ExcelUtil;
import com.newly.common.core.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @anthor sbq
 * @date 2021/10/26 17:37
 */
@RestController
@RequestMapping(ServerUriConstant.NEWLY_BUSINESS_Statistics)
public class StatisticsController implements StatisticsClient {

    @Autowired
    StatisticsService statisticsService;

    @Autowired
    private RequestUtils requestUtils;

    @Override
    public R getSiteData(StatisticsDto statisticsDto) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        return statisticsService.getSiteData(currentTenant, statisticsDto);
    }

    @Override
    public void exportSiteData(StatisticsDto statisticsDto, HttpServletResponse response) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        Map<String, Object> result = (Map<String, Object>) statisticsService.getSiteData(currentTenant, statisticsDto).getData();
        List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("dataList");
        if (ObjectUtil.isEmpty(list)) {
            throw new NewlyException(CodeConstant.FAILED, "无数据");
        }
        List<ExcelSiteData> collect = list.stream().map(map -> {
            ExcelSiteData excel = BeanUtil.mapToBean(map, ExcelSiteData.class, false,
                    CopyOptions.create().setIgnoreProperties("overloadRate", "averageOverRate"));
            excel.setOverloadRate(
                    String.format("%.2f", (double) map.get("overloadRate") * 100) + "%"
            ).setAverageOverRate(
                    String.format("%.2f", (double) map.get("averageOverRate") * 100) + "%"
            );
            return excel;
        }).collect(Collectors.toList());

        ExcelUtil.export(response, "站点超重违法统计明细表", null, collect, ExcelSiteData.class);
    }

    @Override
    public R getTimeData(StatisticsDto statisticsDto) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        return statisticsService.getTimeData(currentTenant, statisticsDto);
    }

    @Override
    public void exportTimeData(StatisticsDto statisticsDto, HttpServletResponse response) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        Map<String, Object> result = (Map<String, Object>) statisticsService.getTimeData(currentTenant, statisticsDto).getData();
        List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("dataList");
        if (ObjectUtil.isEmpty(list)) {
            throw new NewlyException(CodeConstant.FAILED, "无数据");
        }
        List<ExcelTimeData> collect = list.stream().map(map -> {
            ExcelTimeData excel = BeanUtil.mapToBean(map, ExcelTimeData.class, false,
                    CopyOptions.create().setIgnoreProperties("overloadRate", "averageOverRate"));
            excel.setOverloadRate(
                    String.format("%.2f", (double) map.get("overloadRate") * 100) + "%"
            ).setAverageOverRate(
                    String.format("%.2f", (double) map.get("averageOverRate") * 100) + "%"
            );
            return excel;
        }).collect(Collectors.toList());

        ExcelUtil.export(response, "时间超重违法统计明细表", null, collect, ExcelTimeData.class);
    }

    @Override
    public R getAxleData( StatisticsDto statisticsDto) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        return statisticsService.getAxleData(currentTenant, statisticsDto);
    }

    @Override
    public void exportAxleData(StatisticsDto statisticsDto, HttpServletResponse response) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        Map<String, Object> result = (Map<String, Object>) statisticsService.getAxleData(currentTenant, statisticsDto).getData();
        List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("dataList");
        if (ObjectUtil.isEmpty(list)) {
            throw new NewlyException(CodeConstant.FAILED, "无数据");
        }
        List<ExcelAxleData> collect = list.stream().map(map -> {
            ExcelAxleData excel = BeanUtil.mapToBean(map, ExcelAxleData.class, false,
                    CopyOptions.create().setIgnoreProperties("overloadRate", "averageOverRate"));
            String axleNum = excel.getAxleNum();
            excel.setOverloadRate(
                    String.format("%.2f", (double) map.get("overloadRate") * 100) + "%"
            ).setAverageOverRate(
                    String.format("%.2f", (double) map.get("averageOverRate") * 100) + "%"
            ).setAxleNum(
                    "6".equals(axleNum) ? "6轴及以上" : axleNum + "轴"
            );
            return excel;
        }).collect(Collectors.toList());

        ExcelUtil.export(response, "轴数超重违法统计明细表", null, collect, ExcelAxleData.class);
    }

    @Override
    public R getAreaData( StatisticsDto statisticsDto) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        return statisticsService.getAreaData(currentTenant, statisticsDto);
    }

    @Override
    public void exportAreaData( StatisticsDto statisticsDto, HttpServletResponse response) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        Map<String, Object> result = (Map<String, Object>) statisticsService.getAreaData(currentTenant, statisticsDto).getData();
        List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("dataList");
        if (ObjectUtil.isEmpty(list)) {
            throw new NewlyException(CodeConstant.FAILED, "无数据");
        }
        List<ExcelAreaData> collect = list.stream().map(map -> {
            ExcelAreaData excel = BeanUtil.mapToBean(map, ExcelAreaData.class, false,
                    CopyOptions.create().setIgnoreProperties("overloadRate", "averageOverRate"));
            excel.setOverloadRate(
                    String.format("%.2f", (double) map.get("overloadRate") * 100) + "%"
            ).setAverageOverRate(
                    String.format("%.2f", (double) map.get("averageOverRate") * 100) + "%"
            );
            return excel;
        }).collect(Collectors.toList());

        ExcelUtil.export(response, "区域超重违法统计明细表", null, collect, ExcelAreaData.class);
    }

}
